package org.example.examsystem.service;


import org.example.examsystem.entity.Course;
import org.example.examsystem.mapper.CourseMapper;
import org.example.examsystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private  CourseMapper courseMapper;

    public CourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public Page<Course> getCourses(Pageable pageable) {
        int offset = (int) pageable.getOffset();
        int limit = pageable.getPageSize();
        List<Course> courses = courseMapper.getCourses(offset, limit);
        int total = courseMapper.countCourses();
        return new PageImpl<>(courses, pageable, total);
    }

    @Override
    public void addCourse(Course course) {
        courseMapper.addCourse(course);
    }

    @Override
    public void updateCourse(Course course) {
        courseMapper.updateCourse(course);
    }

    @Override
    public void deleteCourse(Integer cno) {
        courseMapper.deleteCourse(cno);
    }

    @Override
    public Course getCourseByName(String courseName) {
        return courseMapper.getCourseByName(courseName);
    }

    @Override
    public List<Course> getCourseList() {
        return courseMapper.getCourseList();
    }
}